<?php
class Asetting {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__dolinkmail();
				break;
			case 2:
				self::__dofacebook();
				break;
			case 3:
				self::__dofeednotify();
				break;
			case 4:
				self::__dogetnotify();
				break;
			case 5:
				self::__dosnslink();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//会员绑定邮件3-5-1
	private static function __dolinkmail() {
		if (!verify::is_email(sview::$ctrl['email'])) {
			sview::$view->assign_error(80003, lang::text(80003));
		}
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if ($user['emailverify'] == 0) {
			if ($user['email'] != sview::$ctrl['email']) {
				$res   = api_uc::checkemail(sview::$ctrl['email']);
				if ($res['result'] != '000') {
					sview::$view->assign_error(60019, lang::text(60019));
				}
			}
			$queue = sprintf('bind_%d', $userid);
			$nums  = (int)cache::getinst()->get($queue);
			if ($nums < 3) {//发送验证邮箱邮件
				api_mailer::__mailsetingssend(sview::$view, $user, sview::$ctrl['email']);
				$expire = strtotime(date('Y-m-d'.' 23:59:59')) - config::$nowtime;
				cache::getinst()->set($queue, $nums+1, $expire);
			}
		}
		sview::$view->display();
	}
	
	//会员绑定FB3-5-2
	private static function __dofacebook() {
		$fbtoken= trim(sview::$ctrl['fbToken']);
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		$res    = api_uc::fb_link($fbtoken, $user['token']);
		if ($res['result'] != '000') {
			sview::$view->assign_error(60026, lang::text(60026));
		}
		api_uc::snstoken($userid, $fbtoken);
		sview::$view->display();
	}
	
	//设置订阅参数信息 3-5-3
	private static function __dofeednotify() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$field  = trim(sview::$ctrl['field']);
		$value  = ((int)sview::$ctrl['value'] == 0)? 0:1;
		//获取资料信息
		$orm    = model::load('live/usersetting')->set_devision($userid);
		$setting= $orm->get_one($userid);
		$type   = (sview::$ctrl['type'] == 1)? 'mailnotify':'pushnotify';
		$flag   = isset($setting[$type])? intval($setting[$type]):0;
		switch ($field) {
			case 'issafe'://账号安全通知
				$sign = 0x01;
				break;
			case 'isfeed'://订阅消息通知
				$sign = 0x02;
				break;
			case 'isnews'://产品新闻通知
				$sign = 0x04;
				break;
			default://所有邮件通知
				$sign = 0x0f; 
				break;
		}
		if ($value == 1) {
			$flag |= $sign;
			if ($flag & 0x01 && $flag & 0x02 && $flag & 0x04) {
				$flag |= 0x08;
			}
		} else {
			$flag &= ~$sign;
			$flag &= ~0x08;
		}
		//更新保存到DB当中
		if ($setting) {
			$orm->save($userid, array($type=>$flag));
		} else {
			$field = array('userid'=>$userid, $type=>$flag);
			$orm->new_one($field);
		}
		sview::$view->display();
	}
	
	//获取通知提醒设置3-5-4
	private static function __dogetnotify() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$setting= model::load('live/usersetting')
			->set_devision($userid)->get_one($userid);
		$data   = array('mailnotify'=>$setting['mailnotify'], 'pushnotify'=>$setting['pushnotify']);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//获取第三方绑定3-5-5
	private static function __dosnslink() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$list   = array();
		$token  = model::load('live/snstoken')->get_one($userid);
		if ($token && $token['fb_token']) {
			$item = api_sns::get_fb_user($token['fb_token']);
			if ($item) {
				$item['type'] = 'facebook';
				$list[]       = $item;
			}
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
}